package com.shaoshaohuo.app.ui;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.IncomeStatementEntity;
import com.shaoshaohuo.app.entity.IncomeStatementEntity.ItemIncome;
import com.shaoshaohuo.app.entity.PayFlow;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.adapter.IncomeStatementAdapter;
import com.shaoshaohuo.app.view.EcDatepickerPopwindow;
import com.shaoshaohuo.app.view.EcDatepickerPopwindow.OnSelectDateListener;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @ClassName IncomeStatementActivity
 * @Decription 收支明细
 * @2015-8-5 下午10:39:57
 */
public class IncomeStatementActivity extends BaseActivity implements OnClickListener, IXListViewListener {

	private int offset = 20;
	private String typeid;
	private String cursor;
	private String month;

	private TopbarView mTopbarView;
	private List<ItemIncome> mDataList = new ArrayList<>(); // 保存 支付详情 的list
	private IncomeStatementAdapter<PayFlow> mAdapter; // listview 填充器
	private TextView mYearView; // 年
	private EcDatepickerPopwindow datePop; // 日期 选择
	private TextView mMonthView; // 月份
	private TextView mPayView; // 支付 金额
	private TextView mIncomeView; // 收入 金额
	private XListView mListView; // 显示详情 listview

	private Calendar calendar = Calendar.getInstance();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_income_statement);
		initView();
		setUpView();
		requestData(true);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		mYearView = (TextView) findViewById(R.id.tv_year);
		mMonthView = (TextView) findViewById(R.id.tv_month);
		mPayView = (TextView) findViewById(R.id.tv_pay);
		mIncomeView = (TextView) findViewById(R.id.tv_income);
		mListView = (XListView) findViewById(R.id.listview);
	}

	private void setUpView() {
		mTopbarView.setCenterText("收支明细");
		mTopbarView.setLeftView(true, true);

		mYearView.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "年");
		mMonthView.setText(String.valueOf(calendar.get(calendar.MONTH) + 1) + "月");
		int monthNum = calendar.get(calendar.MONTH) + 1;
		String monthString = monthNum > 9 ? monthNum + "" : "0"+monthNum;
		month = String.valueOf(calendar.get(Calendar.YEAR)) + "-" + monthString ;

		mMonthView.setOnClickListener(this);

		mListView.setPullLoadEnable(true);
		mListView.setPullRefreshEnable(true);
		mListView.setXListViewListener(this);
	}

	private void requestData(final boolean loading) {

		if (loading) {
			startLoadingDialog();
			mDataList.clear();
			cursor = "0";
			typeid = ListViewAction.NEW1;
			mListView.setPullLoadEnable(true);
		} else {
			cursor = mDataList.size() > 0 ? mDataList.get(mDataList.size() - 1).cursor : "0";
			typeid = ListViewAction.HISTORY1;
		}

		RequestService.getInstance().getIncomeStatement(this, cursor, typeid, offset + "", month,
				IncomeStatementEntity.class, new RequestListener() {
					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						dismissLoadingDialog();
						if (resultData.isOk()) {
							IncomeStatementEntity entity = (IncomeStatementEntity) resultData;
							if (entity.data !=null && entity.data.list != null) {
								Map<String, List<ItemIncome>> map = entity.data.list;
								List<ItemIncome> list = map2List(map);

								if (list.size() < offset) {
									mListView.endLoadingMore("没有更多了");
								}
								mDataList.addAll(list);

							} else {
								mListView.endLoadingMore("没有更多了");
							}

							if (loading) {
								if (entity.data.ortherInfo.totalOut == null) {
									mPayView.setText("0.0");
								} else {
									mPayView.setText(entity.data.ortherInfo.totalOut);
								}
								if (entity.data.ortherInfo.totalIn == null) {
									mIncomeView.setText("0.0");
								} else {
									mIncomeView.setText(entity.data.ortherInfo.totalIn);
								}
							}
							setAdapter(mDataList);

						} else {
							showToast(resultData.getMsg());
						}
						onLoad();
					}

					@Override
					public void onFailed(int requestCode, Exception error, String msg) {
						dismissLoadingDialog();
						showToast(msg);
						onLoad();
					}
				});
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
	}

	/**
	 * map 转成list
	 *
	 * @param map
	 * @return
	 */
	protected List<ItemIncome> map2List(Map<String, List<ItemIncome>> map) {
		ArrayList<ItemIncome> arrayList = new ArrayList<>();
		Set<Entry<String, List<ItemIncome>>> entrySet = map.entrySet();
		for (Entry<String, List<ItemIncome>> entry : entrySet) {
			String key = entry.getKey();
			arrayList.add(new ItemIncome(key));
			arrayList.addAll(entry.getValue());
		}
		return arrayList;
	}

	protected void setAdapter(List<ItemIncome> mDataList2) {
		if (mAdapter == null) {
			mAdapter = new IncomeStatementAdapter<>(this, mDataList2, false);
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.setData(mDataList2);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_month:
			selectDate();
			break;
		}
	}

	// 选择 年 月
	private void selectDate() {
		if (datePop != null && datePop.isShowing()) {
			return;
		}
		if (datePop == null) {

			datePop = new EcDatepickerPopwindow(this);
			datePop.setOnSelectDateListener(new OnSelectDateListener() {
				@Override
				public void onSelectDate(String date) {
					String[] dates = date.split("-");
					mYearView.setText(dates[0]);
					mMonthView.setText(dates[1]);
					month = dates[0] + "-" + dates[1];
					requestData(true);
				}
			});
		}
		datePop.showLocation(mTopbarView);
	}

	@Override
	public void onRefresh() {
		requestData(true);
	}

	@Override
	public void onLoadMore() {
		requestData(false);
	}

}
